#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 09-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Drawing;
using System.ComponentModel;

using Thn;
#endregion

namespace Thn.Interface.Vcl
{
    /// <summary>
    /// The third most popular control for getting user inputs.
    /// </summary>
    [ToolboxItem(true), DefaultProperty("Title"), DefaultEvent("ValueChanged"), ToolboxBitmap(typeof(ResourceBeacon), "Thn.Interface.Vcl.Resources.Icons.TextBox.bmp")]
#if DEBUG
    public
#else
    public
#endif
 class TextBox : EditBox
    {
        #region Fields
        #endregion

        #region Properties

        #region Multiline
        /// <summary>
        /// Gets/Sets whether long text shall be rendered as multiple rows.
        /// </summary>
        [Browsable(true), DefaultValue(false), Category("Appearance"), Description("Whether long text shall be rendered as multiple rows")]
        public bool Multiline
        {
            get { return RenderContext.Multiline; }
            set
            {
                RenderContext.Multiline = value;
                Invalidate();
            }
        }
        #endregion

        #region Alignment
        /// <summary>
        /// Gets or Sets alignment of content.
        /// </summary>
        [Browsable(true), DefaultValue(ContentAlignment.MiddleLeft), Category("Appearance"), Description("How to arrange contents")]
        public ContentAlignment Alignment
        {
            get { return RenderContext.Alignment; }
            set
            {
                RenderContext.Alignment = value;
                InvalidateLayout();
            }
        }
        #endregion

        #region Text Style
        /// <summary>
        /// Gets/Sets font style for rendering text.
        /// </summary>
        [Browsable(true), DefaultValue(TextStyle.Normal), Category("Appearance"), Description("Font style for rendering text")]
        public TextStyle TextStyle
        {
            get { return RenderContext.TextStyle; }
            set
            {
                RenderContext.TextStyle = value;
                Invalidate();
            }
        }
        #endregion

        #region Render Context
        /// <summary>
        /// Gets current render context
        /// </summary>
        protected new TextBoxRenderContext RenderContext
        {
            get { return ((TextBoxRenderContext)base.RenderContext); }
        }
        #endregion

        #region Show Drop Button
        /// <summary>
        /// Gets or Sets value indicate that whether the drop button will be showed or not.
        /// </summary>
        [Browsable(true), DefaultValue(false), Category("Appearance"), Description("Gets or Sets value indicate that whether the drop button will be showed or not.")]
        public bool ShowDropButton
        {
            get { return mShowDropButton; }
            set 
            {
                if (mShowDropButton != value)
                {
                    mShowDropButton = value;
                    if (!DesignMode)
                    {
                        InvalidateLayout();
                    }
                    #region Defensive tracing
                    else
                    {
                        // do not render in runtime mode.
                    }
                    #endregion
                }
                #region Defensive tracing
                else
                {
                    // do not set the same value.
                }
                #endregion
            }
        }
        #endregion  

        #region Allow Context Menu
        bool mAllowContextMenu = true;
        /// <summary>
        /// Gets or Sets value determine whether the context menu allow show or not when user right click on control.
        /// </summary>
        [Browsable(true), DefaultValue(true), Category("Appearance"), Description("Determine whether the context menu allow show or not when user right click on control.")]
        public bool AllowContextMenu
        {
            get 
            {
                return mAllowContextMenu; 
            }
            set 
            {
                mAllowContextMenu = value;
                if (Inner != null)
                {
                    Inner.AllowContextMenu = value;
                }
                #region Defensive tracing
                else
                {
                    // the inner text does not initialized, do nothing.
                }
                #endregion
            }
        }
        #endregion  

        #endregion

        #region Events

        #region Text Changed
        /// <summary>
        /// This event is raised after property Text is modified.
        /// </summary>
        public new event EventHandler TextChanged;

        /// <summary>
        /// Invoke event TextChanged
        /// </summary>
        protected void RaiseTextChanged(EventArgs e)
        {
            EventHandler handler = TextChanged;
            if (handler != null) handler(this, EventArgs.Empty);
        }
        #endregion

        #endregion

        #region On Get Render Context
        /// <summary>
        /// Get reference to the renderation context which is used by <see cref="IControlRenderer"/>.
        /// </summary>
        protected override RenderContext OnGetRenderContext(Graphics canvas)
        {
            RenderContext.DisplayText = this.Text;
            return RenderContext;
        }
        #endregion

        #region On Create Render Context
        /// <summary>
        /// Create a new render context instance.
        /// </summary>
        protected override EditBoxRenderContext OnCreateRenderContext()
        {
            TextBoxRenderContext result = new TextBoxRenderContext();
            result.Alignment = ContentAlignment.MiddleLeft;
            result.Multiline = false;
            result.TextStyle = TextStyle.Normal;

            return result;
        }
        #endregion

        #region On Text Changed
        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            RaiseTextChanged(e);
            RaiseValueChanged(e);
        }
        #endregion

        #region On Load
        /// <summary>
        /// Override this method for set value of allow show context menu.
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (Inner != null)
            {
                Inner.AllowContextMenu = mAllowContextMenu;
            }
            #region Defensive tracing
            else
            {
                // the inner control does not initialized.
            }
            #endregion
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public TextBox()
        {
            
        }
        #endregion
    }
}
